home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Auth / HTTP.php < prev    next >
PHP Script  |  2004-03-24  |  4KB  |  150 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Martin Jansen <mj@php.net>                                  |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Auth_HTTP.php,v 1.9 2003/01/04 11:54:44 mj Exp $
  20. //
  21.  
  22. require_once "Auth/Auth.php";
  23.  
  24. /**
  25.  * PEAR::Auth_HTTP
  26.  *
  27.  * The PEAR::Auth_HTTP class provides methods for creating an
  28.  * HTTP authentication system using PHP.
  29.  *
  30.  * Instead of generating an HTML driven form like PEAR::Auth
  31.  * does, this class sends header commands to the clients which
  32.  * cause them to present a login box like they are e.g. used
  33.  * in Apache's .htaccess mechanism.
  34.  *
  35.  * This class requires the PEAR::Auth package.
  36.  *
  37.  * @author  Martin Jansen <mj@php.net>
  38.  * @package Auth_HTTP
  39.  * @extends Auth
  40.  * @version $Revision: 1.9 $
  41.  */
  42. class Auth_HTTP extends Auth
  43. {
  44.  
  45.     // {{{ properties
  46.  
  47.     /**
  48.      * Name of the realm
  49.      *
  50.      * @access public
  51.      * @var    string
  52.      * @see    drawLogin()
  53.      */
  54.     var $realm = "protected area";
  55.  
  56.     /**
  57.      * Text to send if user hits cancel button
  58.      *
  59.      * @access public
  60.      * @var    string
  61.      * @see    drawLogin()
  62.      */
  63.     var $CancelText = "Error 401 - Access denied";
  64.  
  65.     // }}}
  66.     // {{{ assignData()
  67.  
  68.     /**
  69.      * Assign values from $PHP_AUTH_USER and $PHP_AUTH_PW
  70.      * to internal variables and sets the session id based
  71.      * on them
  72.      *
  73.      * @return void
  74.      */
  75.     function assignData()
  76.     {
  77.         $server = &$this->_importGlobalVariable("server");
  78.  
  79.         if (isset($server['PHP_AUTH_USER']) && $server['PHP_AUTH_USER'] != "") {
  80.             $this->username = $server['PHP_AUTH_USER'];
  81.         }
  82.  
  83.         if (isset($server['PHP_AUTH_PW']) && $server['PHP_AUTH_PW'] != "") {
  84.             $this->password = $server['PHP_AUTH_PW'];
  85.         }
  86.  
  87.         if (isset($this->username) && isset($this->password)) {
  88.             session_id(md5("Auth_HTTP" . $this->username . $this->password));
  89.         }
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ drawLogin()
  94.  
  95.     /**
  96.      * Launch the login box
  97.      *
  98.      * @param  string $username  Username
  99.      * @return void
  100.      */
  101.     function drawLogin($username = "")
  102.     {
  103.         /**
  104.          * Send the header commands
  105.          */
  106.         header("WWW-Authenticate: Basic realm=\"".$this->realm."\"");
  107.         header("HTTP/1.0 401 Unauthorized");
  108.  
  109.         /**
  110.          * This code is only executed if the user hits the cancel
  111.          * button or if he enters wrong data 3 times.
  112.          */
  113.         echo $this->CancelText;
  114.         exit;
  115.     }
  116.  
  117.     // }}}
  118.     // {{{ setRealm()
  119.  
  120.     /**
  121.      * Set name of the current realm
  122.      *
  123.      * @access public
  124.      * @param  string $name  Name of the realm
  125.      * @return void
  126.      */
  127.     function setRealm($name)
  128.     {
  129.         $this->realm = $name;
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ setCancelText()
  134.  
  135.     /**
  136.      * Set the text to send if user hits the cancel button
  137.      *
  138.      * @access public
  139.      * @param  string $text  Text to send
  140.      * @return void
  141.      */
  142.     function setCancelText($text)
  143.     {
  144.         $this->CancelText = $text;
  145.     }
  146.  
  147.     // }}}
  148. }
  149. ?>
  150.